/*
  ! 如果没有天赋，那就一直重复
  ! Created: 2024/05/30 08:57:22
*/
#include <bits/stdc++.h>
using namespace std;

#define x first
#define y second
// #define int long long
#define mp(x, y) make_pair(x, y)
#define eb(...) emplace_back(__VA_ARGS__)
#define fro(i, x, y) for (int i = (x); i <= (y); i++)
#define pre(i, x, y) for (int i = (x); i >= (y); i--)
inline void JYFILE19();

typedef long long i64;
typedef pair<int, int> PII;

bool ST;
const int N = 1e7 + 10;
const int mod = 998244353;

int n, m, a[N], tn[N];
i64 A, B, C, M;

inline i64 power(i64 x, i64 y) {
  i64 res = 1;
  while (y) {
    if (y & 1) res = res * x % mod;
    x = x * x % mod, y /= 2;
  }
  return res;
}
inline bool check(double mid) {
  double las = 0;
  if (a[1] == 1) las = 0.5;
  if (a[1] == 2) las = (1 - mid) / 2;
  fro(i, 2, m) {
    double a1 = max(0., mid - las);
    double a2 = (a[i] == 2 ? mid / 2 : 0);
    if (a1 + a2 > 0.5) return 0;
    las = 0.5 - a1 - a2;
  }
  return 1;
}
inline void solve() {
  cin >> n >> a[0] >> A >> B >> C >> M, m = 0;
  fro(i, 1, n) a[i] = (A * a[i - 1] % M * a[i - 1] + B * a[i - 1] + C) % M + 1, tn[a[i]]++;
  fro(i, 1, M) if (tn[i]) a[++m] = min(tn[i], 2);
  double l = 0, r = 1, ans;
  while (r - l >= 1e-7) {
    double mid = (l + r) / 2;
    if (check(mid)) {
      l = mid, ans = mid;
    } else {
      r = mid;
    }
  }
  printf("%.9lf 1\n", ans);
  fill(tn + 1, tn + M + 1, 0);
}

signed main() {
  JYFILE19();
  int t; cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

bool ED;
inline void JYFILE19() {
  freopen("guess.in", "r", stdin);
  freopen("guess.out", "w", stdout);
  srand(random_device{}());
  ios::sync_with_stdio(0), cin.tie(0);
  double MIB = fabs((&ED - &ST) / 1048576.), LIM = 1024;
  cerr << "MEMORY: " << MIB << endl, assert(MIB <= LIM);
}